export default class DB {
  private dbName: string; // 数据库名称
  private db: any; //数据库对象
  constructor(dbName: string) {
    this.dbName = dbName;
  }

  // 打开数据库
  public openStore(stores: any) {
    const request = window.indexedDB.open(this.dbName, 5);
    return new Promise((resolve, reject) => {
      request.onsuccess = (event: any) => {
        console.log('数据库打开成功');
        this.db = event.target.result;
        console.log(event);
        resolve(true);
      };
      request.onerror = (event: any) => {
        console.log('数据库打开失败');
        console.log(event);
        reject(event);
      };
      request.onupgradeneeded = (event: any) => {
        console.log('数据库升级成功');
        const { result }: any = event.target;
        for (const storeName in stores) {
          // 初始化多个objectStore对象仓库
          const { keyPath, indexs } = stores[storeName];

          if (!result.objectStoreNames.contains(storeName)) {
            // 没有表则新建表
            // keyPath ：主键，主键（key）是默认建立索引的属性；autoIncrement:是否自增；createObjectStore会返回一个对象仓库objectStore
            const store = result.createObjectStore(storeName, {
              autoIncrement: true,
              keyPath,
            });

            store.transaction.oncomplete = (e: any) => {
              if (indexs && indexs.length) {
                indexs.maps((v: string) =>
                  // createIndex可以新建索引，unique字段是否唯一
                  store.createIndex(v, v, { unique: false })
                );
              }
              console.log('创建对象仓库成功');
            };
          }
        }
      };
    });
  }
  // 新增/修改数据库数据
  updateItem(storeName: string, data: any) {
    const store = this.db
      .transaction([storeName], 'readwrite')
      .objectStore(storeName);
    const request = store.put({ ...data, updateTime: new Date().getTime() });
    return new Promise((resolve, reject) => {
      request.onsuccess = (event: any) => {
        console.log('数据写入成功');
        console.log(event);
        resolve(event.target.result);
      };
      request.onerror = (event: any) => {
        console.log('数据写入失败');
        console.log(event);
        reject(event);
      };
    });
  }
  // 删除数据
  deleteItem(storeName: string, key: number | string) {
    const store = this.db
      .transaction([storeName], 'readwrite')
      .objectStore(storeName);
    const request = store.delete(key);
    return new Promise((resolve, reject) => {
      request.onsuccess = (event: any) => {
        console.log('数据删除成功');
        console.log(event);
        resolve(event);
      };
      request.onerror = (event: any) => {
        console.log('数据删除失败');
        console.log(event);
        reject(event);
      };
    });
  }
  // 查询所有数据
  getList(storeName: string) {
    const store = this.db.transaction(storeName).objectStore(storeName);
    const request = store.getAll();
    return new Promise((resolve, reject) => {
      request.onsuccess = (event: any) => {
        console.log('查询所有数据成功');
        console.log(event.target.result);
        resolve(event.target.result);
      };
      request.onerror = (event: any) => {
        console.log('查询所有数据失败');
        console.log(event);
        reject(event);
      };
    });
  }
  // 查询某一条数据
  getItem(storeName: string, key: number | string) {
    const store = this.db.transaction(storeName).objectStore(storeName);
    const request = store.get(key);
    return new Promise((resolve, reject) => {
      request.onsuccess = (event: any) => {
        console.log('查询某一条数据成功');
        console.log(event.target.result);
        resolve(event.target.result);
      };
      request.onerror = (event: any) => {
        console.log('查询某一条数据失败');
        console.log(event);
        reject(event);
      };
    });
  }
}
